import Service from '@/api/httpAxios'

export interface ProcessParamType {
  project_id: string
  offset: number
  limit: number
  fleet_id: string
  url?: string
  autoScaling?: object
  policy_id?: string
  alias_id?: string,
  name?: string,
  type?: string
}

export interface Alias {
  project_id: string
  CreateAliasRequestBody: CreateAli
}

export interface CreateAlias {
  name: string
  description: string
  routing_strategy: {
    fleet_id: string
    type: string
    message: string
  }
  [key: string]: string | { [key: string]: string }
}

export interface CreateAli {
  name: string
  description: string
  type: string
  message: string
  associated_fleets: Array<AsPermission>
  [key: string]: string | Array<AsPermission>
}
export interface AsPermission {
  fleet_id: string
  weight: any
}
export interface modifyAlias {
  project_id: string
  alias_id: string
  UpdateAliasRequestBody: CreateAli
}

export interface aliasMsg {
  project_id: string
  alias_id: string
}

class aliases {
  getAliasesList = (obj: ProcessParamType, obj2: any) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/aliases`
    let params: { [key: string]: string | number } = {}
    for (const key in obj2) {
      if (obj2[key]) {
        params[key] = obj2[key]
      }
    }
    if (obj.offset) params['offset'] = obj.offset - 1
    if (obj.limit) params['limit'] = obj.limit
    return Service.get<any>(url, {
      params
    })
  }

  createAlias = (obj: Alias) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/aliases`
    return Service.post<any>(url, obj.CreateAliasRequestBody)
  }

  updateAlias = (obj: modifyAlias) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/aliases/${obj.alias_id}`
    return Service.put<any>(url, obj.UpdateAliasRequestBody)
  }
  deleteAlias = (obj: aliasMsg) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/aliases/${obj.alias_id}`
    return Service.delete<any>(url)
  }
  getAlias = (obj: aliasMsg) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/aliases/${obj.alias_id}`
    return Service.get<any>(url)
  }
}

export default new aliases()
